/*
mqtt.h
offer mqtt realizy pulisher and subscribe message api
designed by lunanting

-------------------------
2020-05-14 12:00:10
create file
-------------------------

*/
#ifndef __MQTT_H
#define __MQTT_H

#include "../bsp/base_type.h"
#include "linkkit/dev_model_api.h"

/***************************call back function********************/
//deal with reciverd property string
extern void xp_mqtt_recive_property_user_api(const char *msg);




/*************************data type define********************/
//region type
typedef enum{
    region_default_common,
    region_1km_complay,
    region_singapore,
    region_american,
}MQTT_region;

//target message
typedef struct{
    char product_key[21];
    char product_secret[65];
    char device_name[33];
    char device_secret[65];
}MQTT_target;

//mqtt class
typedef struct{
    u8 init;    //object init state
    u8 online;  //4g module online
    u8 connect; //connect state,1=connect,0=cutdown
    int devid;  //master device id
    iotx_linkkit_dev_meta_info_t info;  //master meta info
    MQTT_target target; //connect target message
}MQTT_class;

#include "../../middleware/linkkit/mqtt/mqtt_wrapper.h"
//normal mqtt class
#define SUB_NUB 20
typedef void (*nmqtt_cb)(void *pcontext, void *pclient, iotx_mqtt_event_msg_pt msg);
typedef void (*sub_usr_cb)(void * Nmqtt_sub,char *msg,int msg_len);
typedef struct{
    char *topic;
    void *nmq;
    sub_usr_cb cb;
}Nmqtt_sub;


typedef struct{
    iotx_mqtt_param_t mqtt;
    iotx_mqtt_topic_info_t payload;
    void *client;
    int connect_status;
    Nmqtt_sub sub[SUB_NUB];
    int sub_used;
}Nmqtt_class;

/******************************function extern **********************/
extern void xp_mqtt_loop_thread(void *arg); //mqtt start thread
extern int xp_mqtt_init(MQTT_class *mq);    //mqtt connect ali IOT init
extern int xp_mqtt_config(MQTT_class *mq);    //mqtt  init
extern int xp_mqtt_set_target(MQTT_target *target); //set mqtt target
extern int xp_mqtt_open(MQTT_class *mq);    //open a mqtt
extern int xp_mqtt_connect(MQTT_class *mq); //connect a mqtt server
extern int xp_mqtt_link_available(MQTT_class *mq);  //mqtt link available state
extern void xp_mqtt_close(MQTT_class *mq);  //mqtt close server
extern int xp_mqtt_post_data(MQTT_class *mq,u8 *data,int len);  //mqtt post data
extern int xp_mqtt_post_string(MQTT_class *mq,char *str);   //mqtt post string
extern int xp_mqtt_post_property(MQTT_class *mq,char *property);    //mqtt post property
extern int xp_mqtt_post_event(MQTT_class *mq,char *event_id,char *event_value); //mqtt post event
extern void xp_mqtt_regist_userDealApi(void (*api)(const char*,const char *));  //mqtt user deal api regist
extern void xp_mqtt_region_set(MQTT_region type);   //mqtt region set

//normal mqtt
extern Nmqtt_class *nmqtt_new(char *host,char *client_id,char *usrname,char *passwd);
extern void nmqtt_free(Nmqtt_class *nmq);
extern int nmqtt_connect(Nmqtt_class *nmq);
extern void nmqtt_eventCB_set(Nmqtt_class *nmq,nmqtt_cb cb);
extern void nmqtt_subCB_sets(Nmqtt_class *nmq,sub_usr_cb cb);
extern void nmqtt_subCB_set(Nmqtt_class *nmq,int id,sub_usr_cb cb);
extern int nmqtt_publish(Nmqtt_class *nmq,const char *topic,const char *msg);
extern int nmqtt_subcrib(Nmqtt_class *nmq,const char *topic);
extern int nmqtt_subcrib_id(Nmqtt_class *nmq,int id,const char *topic,nmqtt_cb cb);



extern int xp_mqtt_debug(char *type,char *fun,char *param); //mqtt test and debug
#endif
//end of the file
